package com.bpzj.db.mongo;

import com.bpzj.db.mongo.document.Country;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.ArrayList;
import java.util.List;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @author bpzj 2019/12/4 10:48
 */
public class OriginDriverNoSpring {

  private static Logger log = LoggerFactory.getLogger(OriginDriverNoSpring.class);

  public static void main(String[] args) {
    // 使用官方驱动
    useOfficialJavaDriveClass();
    System.out.println("───────────────────────────────────────────────────────────────────────────");
    System.out.println("───────────────────────────────────────────────────────────────────────────");
    // 使用 Spring-Data 的 MongoTemplate
    useMongoTemplateNoSpring();

  }

  private static void useOfficialJavaDriveClass() {
    // 注册自定义的 Codec ...................................................
    CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
        CodecRegistries.fromProviders(new MyCodecProvider()),
        MongoClient.getDefaultCodecRegistry());
    MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry).build();
    // 1.创建链接
    MongoClient client = new MongoClient("localhost", options);
    // 2.打开数据库 test
    MongoDatabase db = client.getDatabase("test");
    // 3.获取集合 （通过泛型， 指定Class）
    MongoCollection<Country> collection = db.getCollection("test_country", Country.class);
    // 4.使用相关API： insertMany find
    // List<Country> countries = new ArrayList<>();
    // countries.add(new Country(new ObjectId().toString(),"China"));
    // collection.insertMany(countries);
    long start = System.currentTimeMillis();
    FindIterable<Country> documents = collection.find();
    for (Country country : documents) {
      country.getId();
    }
    log.info("官方驱动的 find() 函数得到的 FindIterable 类型， 在对其进行遍历时，才真正查询数据库");
    log.info("使用 “官方驱动” + “自定义Codec” 查询耗时："+ (System.currentTimeMillis() - start));
    client.close();
  }

  /** 单独使用 MongoTemplate */
  public static void useMongoTemplateNoSpring() {
    MongoClient client = new MongoClient("localhost");
    MongoTemplate mongoTemplate = new MongoTemplate(client, "test");
    long start = System.currentTimeMillis();
    List<Country> countries = mongoTemplate.find(Query.query(new Criteria()), Country.class);
    log.info("使用 MongoTemplate + “通用类型转换” 查询耗时："+ (System.currentTimeMillis() - start));
    start = System.currentTimeMillis();
    for (Country country : countries) {
      country.getId();
    }
    log.info("循环耗时："+ (System.currentTimeMillis() - start));
  }
}
